package com.lzhpo.core.utils;

import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * @author <a href="lijing1@wxchina.com@wxchina.com">Lijin</a>
 * @Description TODO
 * @Date 2020/1/14 16:16
 * @Version 1.0
 **/
public class MyDateUtil {

    /**
     * pk01的开始结束时间
     */
    public static  final   String  startTimePk="09:32:00";
    public static  final   String  endTimePk="23:52:59";
    /**
     * 飞艇的开始结束时间
     */
    public static  final   String  startTimeBoat="13:10:15";
    public static  final   String  endTimeBoat="04:05:17";//跨天啦 原来是05：15  增加2分钟延迟



    private static Logger logger = LoggerFactory.getLogger(MyDateUtil.class);

    public static  final  FastDateFormat fastDateFormatYmdhms= FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    public static  final  FastDateFormat fastDateFormat= FastDateFormat.getInstance("HH:mm:ss");



    /**
     * 判断当前时间是否在两个时间间隔之内
     * @param startTimePk
     * @param endTimePk
     */
    public static boolean isTimeValidInnerDay(String startTimePk,
                                                  String endTimePk,
                                                  DataTypeEnum typeEnum) {

        boolean result=false;
        Date startTime = null;
        try {
            startTime = fastDateFormat.parse(startTimePk);
            Date endTime = fastDateFormat.parse(endTimePk);
            String nowStr1= fastDateFormat.format(new Date());
            Date nowTime = fastDateFormat.parse(nowStr1);
            if (typeEnum==DataTypeEnum.BEIJING_BOAT){
                result=isEffectiveDateBoat(nowTime, startTime, endTime);
            }
            logger.info("dataType is {} checkTimeOut- now is {},startTime is {} and endTime is {}  ,check result is {}"
            ,typeEnum.getName(),nowStr1,startTimePk,endTimePk,result);
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("error",e);
        }
        return  result;
    }

    private static boolean isEffectiveDateBoat(Date nowTime,
                                               Date startTime,
                                               Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) || date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
}
